package cn.candy.pet.service.impl;

import cn.candy.basic.exception.BusinessExcpetion;
import cn.candy.basic.service.impl.BaseServiceImpl;
import cn.candy.basic.util.*;
import cn.candy.order.domain.PetAcquisitionOrder;
import cn.candy.order.mapper.PetAcquisitionOrderMapper;
import cn.candy.org.domain.Employee;
import cn.candy.org.domain.Shop;
import cn.candy.org.mapper.EmployeeMapper;
import cn.candy.org.mapper.ShopMapper;
import cn.candy.pet.domain.Pet;
import cn.candy.pet.domain.SearchMasterMsg;
import cn.candy.pet.mapper.SearchMasterMsgMapper;
import cn.candy.pet.query.SearchMasterMsgQuery;
import cn.candy.pet.service.IPetService;
import cn.candy.pet.service.ISearchMasterMsgService;
import cn.candy.user.domain.Logininfo;
import cn.candy.user.domain.User;
import cn.candy.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 寻主消息业务实现类
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IPetService petService;

    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;


    @Override
    public List<SearchMasterMsg> getAll() {
        return searchMasterMsgMapper.loadAll();
    }

    @Override
    public void publish(SearchMasterMsg msg, HttpServletRequest request) {
        //1.校验
        //1.1.空值校验
        if(msg.getTitle().isEmpty() || msg.getAddress().isEmpty()){
            throw new BusinessExcpetion("地址和标题不能为空!!!");
        }

        //2.获取登录用户的信息
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        User user = userMapper.findByLogininfoId(logininfo.getId());
        msg.setUser(user);
        msg.setUser_id(user.getId());
        //3.通过工具类获取到最近的店铺的id
        Point point = DistanceUtil.getPoint(msg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());
        msg.setShop(nearestShop);
        msg.setShop_id(nearestShop.getId());

        //4.添加寻主消息
        searchMasterMsgMapper.save(msg);

        //5.给店铺管理员发送短信 - 作弊
        Employee employee = employeeMapper.loadById(nearestShop.getAdmin_id());
        //SmsUtil.sendMsg(employee.getPhone(),"你有新的订单，请到【" + msg.getAddress() + "】去收取宠物" + msg.getName());
        System.out.println("你有新的订单，请到【" + msg.getAddress() + "】去收取宠物" + msg.getName());

        //6.如果price有值，创建订单 @TODO

    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        //查询待处理消息
        searchMasterMsgQuery.setState(0);//待处理
        //获取登录的管理员信息：平台 or 店铺
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        //根据登录信息id 查询 管理员
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        //如果管理员有shop_id = 店铺管理员
        if(employee.getShop_id() != null){//店铺管理员
            //店铺管理员只能查询自己店铺的待处理消息
            searchMasterMsgQuery.setShopId(employee.getShop_id());
        }

        return super.queryPage(searchMasterMsgQuery);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        //查询待处理消息
        searchMasterMsgQuery.setState(1);//待处理
        //获取登录的管理员信息：平台 or 店铺
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        //根据登录信息id 查询 管理员
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        //如果管理员有shop_id = 店铺管理员
        if(employee.getShop_id() != null){//店铺管理员
            //店铺管理员只能查询自己店铺的待处理消息
            searchMasterMsgQuery.setShopId(employee.getShop_id());
        }
        return super.queryPage(searchMasterMsgQuery);
    }

    @Override
    public PageList<SearchMasterMsg> user(SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request) {
        //用户查询所有消息：待处理 + 已处理
        //获取登录的管理员信息：平台 or 店铺
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        //根据登录信息id 查询 管理员
        User user = userMapper.findByLogininfoId(logininfo.getId());
        searchMasterMsgQuery.setUserId(user.getId());

        return super.queryPage(searchMasterMsgQuery);
    }

    @Override
    public void handle(Pet pet, HttpServletRequest request) {
        //1.待处理变成以处理消息 - 修改state=1通过 待处理消息的id
        searchMasterMsgMapper.updateStateById(pet.getSearch_master_msg_id());
        //2.添加宠物相关的信息
        petService.add(pet,request);
        //2.1.添加宠物信息
        //2.2.添加宠物明细
        //3.创建订单
        //3.1获取寻主消息对象
        SearchMasterMsg masterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        Logininfo logininfo = LoginContextUtil.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());

        PetAcquisitionOrder acquisitionOrder = pet2order(pet, masterMsg, employee.getId());
        //添加收购订单
        petAcquisitionOrderMapper.save(acquisitionOrder);

        //4.支付 @TODO
    }


    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg masterMsg,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付 以支付1 取消-1
        order.setPrice(pet.getCostprice());
        order.setAddress(masterMsg.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(masterMsg.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(masterMsg.getUser_id());
        order.setPaytype(pet.getPayType());
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        //lastcomfirmtime - 店铺给用户 - 不用倒计时定时器
        //paySn - 支付单唯一单号 - 支付的时候做
        return order;
    }
}
